package com.xiaotao.learn.algorithm.search;

public class BinarySearch {
    public static void main(String[] args) {
        System.out.println(search(new int[]{1,2,3,4,5,6,7,8,10}, 9));
    }

    public static int search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int mid;
        while (left <= right) {
            mid = (left + right)/2;
            System.out.println(left + " " + right);
            System.out.println(mid);
            if (target == nums[mid]) {
                return mid;
            }
            if (target > nums[mid]) {
                left = mid + 1;
            } else if(right != left) {
                right = mid;
            } else {
                break;
            }
        }
        return left;
    }
}
